package hhm.android.library.weightHeightPicker.heightPicker;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import hhm.android.library.R;
import hhm.android.library.utils.GetStringListener;

import static hhm.android.library.weightHeightPicker.heightPicker.HeightParams.TYPE_CM;
import static hhm.android.library.weightHeightPicker.heightPicker.HeightParams.TYPE_FT;
import static hhm.android.library.weightHeightPicker.heightPicker.HeightParams.TYPE_IN;
import static hhm.android.library.weightHeightPicker.heightPicker.HeightParams.TYPE_MM;

public class HeightPickDialog extends Dialog {

    private TextView cancelText;
    private TextView sureText;
    private HeightPickerView mHeightPickerView;
    private Builder mBuilder;

    private HeightPickDialog(Context context, Builder builder) {
        super(context, R.style.dialog_style);
        mBuilder = builder;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cbk_dialog_pick_height);
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = outMetrics.widthPixels;
        getWindow().setAttributes(params);

        initView();
    }

    private void initView() {

        mHeightPickerView = findViewById(R.id.wheelLayout);
        HeightParams dateParams = new HeightParams();
        dateParams.types = new int[2];
        if (mBuilder.type.equals("CM")) {
            dateParams.types[0] = TYPE_CM;
            dateParams.types[1] = TYPE_MM;
        } else if (mBuilder.type.equals("FT")) {
            dateParams.types[0] = TYPE_FT;
            dateParams.types[1] = TYPE_IN;
            TextView tvSmall = findViewById(R.id.cbk_dialog_pick_height_class);
            TextView tvBig  = findViewById(R.id.cbk_dialog_pick_height_point);
            tvBig.setVisibility(View.INVISIBLE);
            tvSmall.setVisibility(View.INVISIBLE);
        }

        if (mBuilder.initData != -1) {
            mHeightPickerView.show(mBuilder.type, mBuilder.initData, dateParams, mBuilder.selectColor);
        } else {
            if (mBuilder.type.equals("CM")) {
                mHeightPickerView.show(mBuilder.type, 20.0, dateParams, mBuilder.selectColor);
            } else {
                mHeightPickerView.show(mBuilder.type, 0.08, dateParams, mBuilder.selectColor);
            }
            if (dateParams.types == null) {
                return;
            }
        }

        cancelText = findViewById(R.id.cancel);
        cancelText.setOnClickListener(v -> dismiss());

        sureText = findViewById(R.id.sure);
        sureText.setOnClickListener(v -> {
            dismiss();
            if (mBuilder.onSureListener != null) {
                mBuilder.onSureListener.getString(mHeightPickerView.getChildHeight());
            }
        });
    }

    public static class Builder {
        private GetStringListener onSureListener;
        private int selectColor;
        private double initData = -1;
        private String type;

        public Builder(String type) {
            this.type = type;
        }

        public Builder setOnSureListener(GetStringListener listener) {
            this.onSureListener = listener;
            return this;
        }

        public HeightPickDialog.Builder setInitData(double data) {
            this.initData = data;
            return this;
        }

        public Builder setTextColor(int selectColor) {
            this.selectColor = selectColor;
            return this;
        }

        public void show(Context context) {
            HeightPickDialog dialog = new HeightPickDialog(context, this);
            dialog.show();
        }
    }
}
